package com.wangzhixuan.util;

import java.net.URI;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;


public class HttpUtils {

	/**
	 * Get the request IP
	 * 
	 * @param request
	 * @return
	 */
	public static String getIp(HttpServletRequest request) {
		String ip = request.getHeader("X-Real-IP");
		if (StringUtils.isBlank(ip)) {
			ip = request.getHeader("Host");
		}
		if (StringUtils.isBlank(ip)) {
			ip = request.getHeader("X-Forwarded-For");
		}
		if (StringUtils.isBlank(ip)) {
			ip = "0.0.0.0";
		}
		return ip;
	}

	/**
	 * Get the base path
	 * 
	 * @param request
	 * @return
	 */
	public static String getBasePath(HttpServletRequest request) {
		String path = request.getContextPath();
		String basePath = request.getScheme() + "://" + request.getServerName()
				+ ":" + request.getServerPort() + path;
		return basePath;
	}

	/**
	 * Get the context path
	 * 
	 * @param request
	 * @return
	 */
	public static String getContextPath(HttpServletRequest request) {
		String path = request.getContextPath();
		return path;
	}

        /**
         * get root domain from url, news.baidu.com.cn will return baidu.com.cn
         * @param url
         * @return 
         */
        public static String getRootDomainOfUrl(String url) {
            String root = "";
            if(StringUtils.isEmpty(url)){
                return root;
            }
            try {
                URI uri = new URI(url);
                String host = uri.getHost();
                if (!StringUtils.isEmpty(host)) {
                    String[] hostArray = host.split("\\.");
                    int length = hostArray.length;
                    if (length <= 1) {
                        return root;
                    }
                    String ext = hostArray[length - 1];
                    if (length == 2 || StringUtils.isNumeric(ext)) {
                        return host;
                    } else if (hostArray[length - 2].equals("com")) {
                        //baidu.com.cn domains like this
                        return hostArray[length - 3] + "." + hostArray[length - 2] + "." + hostArray[length - 1];
                    } else {
                        return hostArray[length - 2] + "." + hostArray[length - 1];
                    }
                }
            } catch (Exception e) {
                System.out.println(e);
            }
            return root;
        }
	public static String getAddr(HttpServletRequest request){
		String ip=request.getHeader("x-forwarded-for");
		       if(ip==null || ip.length()==0 || "unknown".equalsIgnoreCase(ip)){
		          ip=request.getHeader("Proxy-Client-IP");
		        }
		      if(ip==null || ip.length()==0 || "unknown".equalsIgnoreCase(ip)){
		         ip=request.getHeader("WL-Proxy-Client-IP");
		       }
		  if(ip==null || ip.length()==0 || "unknown".equalsIgnoreCase(ip)){
		         ip=request.getRemoteAddr();
		       }

		    return ip;

		 }
	public static String getFullURL(HttpServletRequest request) {
		StringBuffer requestURL = request.getRequestURL();
		String queryString = request.getQueryString();

		if (queryString == null) {
			return requestURL.toString();
		} else {
			return requestURL.append('?').append(queryString).toString();
		}
	}
}
